package com.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.model.Apicultor;

import com.model.ApiarioFlora;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table APIARIO_FLORA.
*/
public class ApiarioFloraDao extends AbstractDao<ApiarioFlora, Long> {

    public static final String TABLENAME = "APIARIO_FLORA";

    /**
     * Properties of entity ApiarioFlora.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, long.class, "id", true, "_idFloraApi");
        public final static Property NApiario = new Property(1, String.class, "nApiario", false, "N_APIARIO");
        public final static Property NivExistencia = new Property(2, Integer.class, "nivExistencia", false, "NIV_EXISTENCIA");
        public final static Property Obs = new Property(3, String.class, "obs", false, "OBS");
        public final static Property Iniciof = new Property(4, Integer.class, "iniciof", false, "INICIOF");
        public final static Property Fim = new Property(5, Integer.class, "fim", false, "FIM");
        public final static Property Nome = new Property(6, String.class, "nome", false, "NOME");
        public final static Property ConficProdutivo = new Property(7, Integer.class, "conficProdutivo", false, "CONFIC_PRODUTIVO");
        public final static Property Bi = new Property(8, Integer.class, "bi", false, "BI");
        public final static Property CodAssocia = new Property(9, Integer.class, "codAssocia", false, "COD_ASSOCIA");
        public final static Property NSocio = new Property(10, Integer.class, "nSocio", false, "N_SOCIO");
        public final static Property PlantaId = new Property(11, long.class, "plantaId", false, "PLANTA_ID");
        public final static Property Date = new Property(12, java.util.Date.class, "date", false, "DATE");
    };

    private DaoSession daoSession;

    private Query<ApiarioFlora> apicultor_FlotaA_FloraQuery;

    public ApiarioFloraDao(DaoConfig config) {
        super(config);
    }
    
    public ApiarioFloraDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'APIARIO_FLORA' (" + //
                "'_idFloraApi' INTEGER PRIMARY KEY ASC AUTOINCREMENT NOT NULL ," + // 0: id
                "'N_APIARIO' TEXT," + // 1: nApiario
                "'NIV_EXISTENCIA' INTEGER," + // 2: nivExistencia
                "'OBS' TEXT," + // 3: obs
                "'INICIOF' INTEGER," + // 4: iniciof
                "'FIM' INTEGER," + // 5: fim
                "'NOME' TEXT," + // 6: nome
                "'CONFIC_PRODUTIVO' INTEGER," + // 7: conficProdutivo
                "'BI' INTEGER," + // 8: bi
                "'COD_ASSOCIA' INTEGER," + // 9: codAssocia
                "'N_SOCIO' INTEGER," + // 10: nSocio
                "'PLANTA_ID' INTEGER NOT NULL ," + // 11: plantaId
                "'DATE' INTEGER);"); // 12: date
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'APIARIO_FLORA'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, ApiarioFlora entity) {
        stmt.clearBindings();
        stmt.bindLong(1, entity.getId());
 
        String nApiario = entity.getNApiario();
        if (nApiario != null) {
            stmt.bindString(2, nApiario);
        }
 
        Integer nivExistencia = entity.getNivExistencia();
        if (nivExistencia != null) {
            stmt.bindLong(3, nivExistencia);
        }
 
        String obs = entity.getObs();
        if (obs != null) {
            stmt.bindString(4, obs);
        }
 
        Integer iniciof = entity.getIniciof();
        if (iniciof != null) {
            stmt.bindLong(5, iniciof);
        }
 
        Integer fim = entity.getFim();
        if (fim != null) {
            stmt.bindLong(6, fim);
        }
 
        String nome = entity.getNome();
        if (nome != null) {
            stmt.bindString(7, nome);
        }
 
        Integer conficProdutivo = entity.getConficProdutivo();
        if (conficProdutivo != null) {
            stmt.bindLong(8, conficProdutivo);
        }
 
        Integer bi = entity.getBi();
        if (bi != null) {
            stmt.bindLong(9, bi);
        }
 
        Integer codAssocia = entity.getCodAssocia();
        if (codAssocia != null) {
            stmt.bindLong(10, codAssocia);
        }
 
        Integer nSocio = entity.getNSocio();
        if (nSocio != null) {
            stmt.bindLong(11, nSocio);
        }
        stmt.bindLong(12, entity.getPlantaId());
 
        java.util.Date date = entity.getDate();
        if (date != null) {
            stmt.bindLong(13, date.getTime());
        }
    }

    @Override
    protected void attachEntity(ApiarioFlora entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public ApiarioFlora readEntity(Cursor cursor, int offset) {
        ApiarioFlora entity = new ApiarioFlora( //
            cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // nApiario
            cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2), // nivExistencia
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // obs
            cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4), // iniciof
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // fim
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // nome
            cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7), // conficProdutivo
            cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8), // bi
            cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9), // codAssocia
            cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10), // nSocio
            cursor.getLong(offset + 11), // plantaId
            cursor.isNull(offset + 12) ? null : new java.util.Date(cursor.getLong(offset + 12)) // date
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, ApiarioFlora entity, int offset) {
        entity.setId(cursor.getLong(offset + 0));
        entity.setNApiario(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setNivExistencia(cursor.isNull(offset + 2) ? null : cursor.getInt(offset + 2));
        entity.setObs(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setIniciof(cursor.isNull(offset + 4) ? null : cursor.getInt(offset + 4));
        entity.setFim(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setNome(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setConficProdutivo(cursor.isNull(offset + 7) ? null : cursor.getInt(offset + 7));
        entity.setBi(cursor.isNull(offset + 8) ? null : cursor.getInt(offset + 8));
        entity.setCodAssocia(cursor.isNull(offset + 9) ? null : cursor.getInt(offset + 9));
        entity.setNSocio(cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10));
        entity.setPlantaId(cursor.getLong(offset + 11));
        entity.setDate(cursor.isNull(offset + 12) ? null : new java.util.Date(cursor.getLong(offset + 12)));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(ApiarioFlora entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(ApiarioFlora entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "flotaA_Flora" to-many relationship of Apicultor. */
    public List<ApiarioFlora> _queryApicultor_FlotaA_Flora(long plantaId) {
        synchronized (this) {
            if (apicultor_FlotaA_FloraQuery == null) {
                QueryBuilder<ApiarioFlora> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.PlantaId.eq(null));
                queryBuilder.orderRaw("DATE ASC");
                apicultor_FlotaA_FloraQuery = queryBuilder.build();
            }
        }
        Query<ApiarioFlora> query = apicultor_FlotaA_FloraQuery.forCurrentThread();
        query.setParameter(0, plantaId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getApicultorDao().getAllColumns());
            builder.append(" FROM APIARIO_FLORA T");
            builder.append(" LEFT JOIN APICULTOR T0 ON T.'PLANTA_ID'=T0.'_idApicultor'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected ApiarioFlora loadCurrentDeep(Cursor cursor, boolean lock) {
        ApiarioFlora entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        Apicultor apicultor = loadCurrentOther(daoSession.getApicultorDao(), cursor, offset);
         if(apicultor != null) {
            entity.setApicultor(apicultor);
        }

        return entity;    
    }

    public ApiarioFlora loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<ApiarioFlora> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<ApiarioFlora> list = new ArrayList<ApiarioFlora>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<ApiarioFlora> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<ApiarioFlora> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
